package com.gssm.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gssm.dao.AdminDaoImpl;
import com.gssm.dao.UserDaoImpl;
import com.gssm.dao.YuangongDaoImpl;
import com.gssm.entity.Admin;
import com.gssm.entity.User;
import com.gssm.entity.Yuangong;
import com.gssm.util.Pager;
import com.gssm.util.MailUtil;
import java.util.Properties;

@Controller
public class LoginController extends BaseController{

	@Autowired
	private AdminDaoImpl adminDao;
	
	public AdminDaoImpl getAdminDao() {
		return adminDao;
	}

	public void setAdminDao(AdminDaoImpl adminDao) {
		this.adminDao = adminDao;
	}
	@Autowired
	private YuangongDaoImpl yuangongDao;

	public YuangongDaoImpl getYuangongDao() {
		return yuangongDao;
	}

	public void setYuangongDao(YuangongDaoImpl yuangongDao) {
		this.yuangongDao = yuangongDao;
	}
	
	@Autowired
	private UserDaoImpl userDao;

	public UserDaoImpl getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDaoImpl userDao) {
		this.userDao = userDao;
	}
	/**
	 * 登陆接口
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/login.action")
	public String list(@RequestParam String userName, @RequestParam String userPw, @RequestParam Integer userType, @RequestParam("mailCode") String mailCode, Model model, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		String code = (String) session.getAttribute("mailCode");
		Long time = (Long) session.getAttribute("mailCodeTime");
		if (code == null || !code.equals(mailCode) || time == null || System.currentTimeMillis() - time > 5*60*1000) {
			request.setAttribute("msg", "验证码错误或已过期");
			return "login";
		}
		System.out.println("userType" + userType);
		try {
			Thread.sleep(700);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if (userType == 0)//
		{
			Admin admin = new Admin();
			admin.setUsername(userName);
			admin.setUserpwd(userPw);
			Pager<Admin> pagers = adminDao.findByEntity(admin);
			if (pagers.getTotal() == 0) {
				request.setAttribute("msg", "用户名或者密码错误!");
				return "login";
			} else {
				admin = (Admin) pagers.getDatas().get(0);
				session.setAttribute("userType", 0);
				session.setAttribute("admin", admin);
				session.removeAttribute("mailCode");
				session.removeAttribute("mailCodeTime");
				return "admin/index";
			}
		}
		if (userType == 1)//用户登录
		{
			Yuangong user = new Yuangong();
			user.setLoginname(userName);
			user.setPwd(userPw);
			Pager<Yuangong> pagers = yuangongDao.findByEntity(user);
			if (pagers.getTotal() == 0) {
				request.setAttribute("msg", "用户名或者密码错误!");
				return "login";
			} else {
				user = (Yuangong) pagers.getDatas().get(0);
				session.setAttribute("userType", 1);
				session.setAttribute("yuangong", user);
				session.removeAttribute("mailCode");
				session.removeAttribute("mailCodeTime");
				return "admin/index";
			}
		}
		if (userType == 2)//用户登录
		{
			User user = new User();
			user.setLoginname(userName);
			user.setPwd(userPw);
			Pager<User> pagers = userDao.findByEntity(user);
			if (pagers.getTotal() == 0) {
				request.setAttribute("msg", "用户名或者密码错误!");
				return "login";
			} else {
				user = (User) pagers.getDatas().get(0);
				session.setAttribute("userType", 2);
				session.setAttribute("user", user);
				session.removeAttribute("mailCode");
				session.removeAttribute("mailCodeTime");
				return "admin/index";
			}
		}
		return "admin/index";
	}

	@RequestMapping("/sendMailCode")
	@ResponseBody
	public String sendMailCode(@RequestParam("email") String email, HttpSession session) {
		String code = String.valueOf((int)((Math.random()*9+1)*100000));
		session.setAttribute("mailCode", code);
		session.setAttribute("mailCodeTime", System.currentTimeMillis());
		try {
			Properties mailProps = new Properties();
			mailProps.load(getClass().getClassLoader().getResourceAsStream("jdbc.properties"));
			MailUtil.sendMail(email, code, mailProps);
			return "{\"status\":0,\"msg\":\"验证码已发送\"}";
		} catch (Exception e) {
			return "{\"status\":1,\"msg\":\"发送失败\"}";
		}
	}
}